/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gateway.server.config;

import com.gateway.register.client.server.api.GatewayClientServerRegisterRepository;
import com.gateway.register.client.server.nacos.NacosClientServerRegisterRepository;
import com.gateway.register.common.config.GatewayRegisterCenterConfig;
import com.gateway.server.disruptor.RegisterClientServerDisruptorPublisher;
import com.gateway.server.service.GatewayClientRegisterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * The type Register center configuration.
 */
@Configuration
public class RegisterCenterConfiguration {

    /**
     * gateway register center config gateway register center config.
     *
     * @return the gateway register center config
     */
    @Bean
    @ConfigurationProperties(prefix = "gateway.register")
    public GatewayRegisterCenterConfig gatewayRegisterCenterConfig() {
        return new GatewayRegisterCenterConfig();
    }
    
    /**
     * gateway client server register repository server register repository.
     *
     * @param gatewayRegisterCenterConfig the gateway register center config
     * @param gatewayClientRegisterServices the gateway client register service
     * @return the gateway server register repository
     */
    @Bean(destroyMethod = "close")
    public GatewayClientServerRegisterRepository gatewayClientServerRegisterRepository(final GatewayRegisterCenterConfig gatewayRegisterCenterConfig,
                                                                                     final List<GatewayClientRegisterService> gatewayClientRegisterServices,
                                                                                     final ObjectProvider<List<GatewayClientServerRegisterRepository>> registerRepositories) {
        String registerType = gatewayRegisterCenterConfig.getRegisterType();
        List<GatewayClientServerRegisterRepository> repositories = registerRepositories.getIfAvailable();
        GatewayClientServerRegisterRepository registerRepository = repositories.stream()
                .filter(e -> StringUtils.equalsIgnoreCase(e.registerType(), registerType))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("no such register repository: " + registerType));
        RegisterClientServerDisruptorPublisher publisher = RegisterClientServerDisruptorPublisher.getInstance();
        Map<String, GatewayClientRegisterService> registerServiceMap = gatewayClientRegisterServices.stream().collect(Collectors.toMap(GatewayClientRegisterService::rpcType, e -> e));
        publisher.start(registerServiceMap);
        registerRepository.init(publisher, gatewayRegisterCenterConfig);
        return registerRepository;
    }

    @Bean
    public NacosClientServerRegisterRepository nacosClientServerRegisterRepository() {
        return new NacosClientServerRegisterRepository();
    }
}
